package com.mobile.service.impl;

import com.github.pagehelper.PageInfo;
import com.mobile.service.convert.SupplementInfoConverter;
import com.mobile.service.dao.DaoSupport;
import com.mobile.service.dto.SupplementInfoDTO;
import com.mobile.service.dto.SupplementInfoQueryDTO;
import com.mobile.service.entity.SupplementInfo;
import com.mobile.service.inter.SupplementInfoService;
import com.mobile.service.mapper.SupplementInfoMapper;
import com.mobile.service.util.PageUtils;
import com.mobile.util.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * 补正信息表service实现类
 */
@Service("supplementInfoService")
@Transactional
public class SupplementInfoServiceImpl implements SupplementInfoService {
    @Resource(name = "daoSupport")
    private DaoSupport dao;
    @Autowired
    private SupplementInfoConverter supplementInfoConverter;
    @Autowired
    private SupplementInfoMapper supplementInfoMapper;

    @Override
    public Map<String, Object> addSupplementInfo(SupplementInfoDTO dto) throws Exception {
        Map<String, Object> map = new HashMap<>();
        supplementInfoMapper.insertSupplementInfo(supplementInfoConverter.toEntity(dto));
        map.put(Const.ACTION_RESULT, Const.ACTION_RESULT_SUCCESS);
        map.put(Const.ACTION_MESSAGE, MessageUtil.getMessage("I0011"));
        return map;
    }

    @SuppressWarnings("unchecked")
    @Override
    public PageResult<SupplementInfoDTO> getSupplementInfoListPage(SupplementInfoQueryDTO queryDTO) throws Exception {
        PageUtils.startPage(queryDTO);
        List<SupplementInfoDTO> list = supplementInfoMapper.supplementInfolistPage(queryDTO);
        return new PageResult<SupplementInfoDTO>(list);
    }

    @Override
    public SupplementInfoDTO getSupplementInfoById(Integer id) throws Exception {
        return supplementInfoMapper.getSupplementInfoById(id);
    }

    @Override
    public Map<String, Object> updateSupplementInfo(SupplementInfoDTO dto) throws Exception {
        Map<String, Object> map = new HashMap<>();
        Integer pkValue = dto.getSupplementInfoId();
        if (pkValue == null) {
            map.put(Const.ACTION_RESULT, Const.ACTION_RESULT_FAILED);
            map.put(Const.ACTION_MESSAGE, MessageUtil.getMessage("E0001"));
            return map;
        }

        SupplementInfoDTO data = this.getSupplementInfoById(pkValue);
        if (Objects.isNull(data)) {
            map.put(Const.ACTION_RESULT, Const.ACTION_RESULT_FAILED);
            map.put(Const.ACTION_MESSAGE, MessageUtil.getMessage("E0001"));
            return map;
        }

        supplementInfoMapper.updateSupplementInfo(supplementInfoConverter.toEntity(dto));
        map.put(Const.ACTION_RESULT, Const.ACTION_RESULT_SUCCESS);
        map.put(Const.ACTION_MESSAGE, MessageUtil.getMessage("I0009"));
        return map;
    }

    @Override
    public Map<String, Object> removeSupplementInfo(Integer id) throws Exception {
        Map<String, Object> map = new HashMap<>();
        supplementInfoMapper.deleteSupplementInfo(id);
        map.put(Const.ACTION_RESULT, Const.ACTION_RESULT_SUCCESS);
        map.put(Const.ACTION_MESSAGE, MessageUtil.getMessage("I0010"));
        return map;
    }

    @SuppressWarnings("unchecked")
    @Override
    public List<SupplementInfoDTO> getSupplementInfoList(SupplementInfoQueryDTO queryDTO) throws Exception {
        return supplementInfoMapper.getSupplementInfoList(queryDTO);
    }
}
